//
//  MineViewController.m
//  JewelryStore
//
//  Created by apple on 2016/10/31.
//  Copyright © 2016年 com. All rights reserved.
//

#import "MineViewController.h"
#import "MineHeadCollectionView.h"
#import "MineCollectionViewCell.h"

#define MineItemCell @"mineItemCell"
#define MineHeadCell @"mineHeadCell"

@interface MineViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *mineCollectionView;
@property (nonatomic, strong) UIView *topLoginView;  //顶部登录
@property (nonatomic, strong) UIView *topUnLoginView; //顶部未登录

@property (nonatomic, strong) MineModel *model;
@end

@implementation MineViewController

#pragma mark - viewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setupNavigationBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
    [self initUI];
}

#pragma mark - initData

- (void)initData
{
    NSString *fliePath = [[NSBundle mainBundle] pathForResource:@"MineJson" ofType:@"json"];
    NSData *jdata = [[NSData alloc] initWithContentsOfFile:fliePath];
    [MineModel setup];
    _model = [MineModel mj_objectWithKeyValues:jdata];
}

#pragma mark - initUI

- (void)initUI
{
    BOOL islogin = YES;
    if (islogin) {
        [self initLoginTopUI];
    }else
    {
        [self initLoginTopUI];
    }
    
    [self.view addSubview:self.mineCollectionView];
}

- (void)initLoginTopUI
{
    //backView
    _topLoginView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/2.8)];
    [self.view addSubview:_topLoginView];
    
    //backgroundImage
    UIImageView *backImageV = [[UIImageView alloc] initWithImage:KImageNamed(@"backgroundImage")];
    [_topLoginView addSubview:backImageV];
    [backImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.and.top.and.bottom.equalTo(_topLoginView);
    }];
    
    //头像
    UIButton *headBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    headBtn.layer.cornerRadius = 46;
//    headBtn.layer.masksToBounds = YES;
//    headBtn.backgroundColor = [UIColor orangeColor];
    [headBtn setBackgroundImage:KImageNamed(@"m_touxiang") forState:UIControlStateNormal];
    [headBtn addTarget:self action:@selector(headClick) forControlEvents:UIControlEventTouchUpInside];
    [_topLoginView addSubview:headBtn];
    [headBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.top.equalTo(@60);
        make.height.and.width.equalTo(@(SCREEN_WIDTH/4));
    }];
    
    //userName
    UILabel *userName = [[UILabel alloc] init];
    userName.text = @"阿狸";
    userName.font = Font(22);
    [userName setTextColor:kWhite];
    [_topLoginView addSubview:userName];
    [userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headBtn.mas_right).offset(20);
        make.top.equalTo(headBtn.mas_top).offset(15);
        make.width.equalTo(@(SCREEN_WIDTH-100));
        make.height.equalTo(@25);
    }];
    
    //phoneNumber
    UILabel *phoneNumber = [[UILabel alloc] init];
    phoneNumber.text = @"18850524787";
    [phoneNumber setTextColor:kWhite];
    [_topLoginView addSubview:phoneNumber];
    [phoneNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userName.mas_bottom).offset(5);
        make.left.and.right.and.height.equalTo(userName);
    }];

    //description 简介
    UIImageView *backImageView = [[UIImageView alloc] initWithImage:KImageNamed(@"descriptionBI")];
    [_topLoginView addSubview:backImageView];
    [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.and.bottom.equalTo(@-15);
        make.top.equalTo(headBtn.mas_bottom).offset(10);
        make.left.equalTo(@20);
    }];
    
    UILabel *contentLab = [[UILabel alloc] init];
    contentLab.text = @"这家伙很蠢，什么都没留住😂家家嘎嘎";
    contentLab.numberOfLines = 0;
    contentLab.font = Font(18);
    [contentLab setTextColor:kWhite];
    [backImageView addSubview:contentLab];
    
    UIButton *bIbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [bIbtn setBackgroundImage:KImageNamed(@"m_jianjie") forState:UIControlStateNormal];
    [bIbtn addTarget:self action:@selector(descriptionClick) forControlEvents:UIControlEventTouchUpInside];
    [backImageView addSubview:bIbtn];
    [bIbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.equalTo(@10);
        make.width.and.height.equalTo(@20);
    }];
    __block NSNumber *num = contentLab.text.length < 30 ? @10 : @0;
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bIbtn.mas_right).offset(5);
        make.right.equalTo(backImageView);
        make.top.equalTo(num);
        make.bottom.lessThanOrEqualTo(@0);
    }];

}

- (void)initUnLoginTopUI
{

}

#pragma mark - UICollectionView

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _model.mineData.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[_model.mineData[section] detailData] count];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH/4.04, SCREEN_WIDTH/5.7);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH/9.5);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MineItemCell forIndexPath:indexPath];
    cell.model = [_model.mineData[indexPath.section] detailData][indexPath.row];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headView;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        MineHeadCollectionView *tHeadView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:MineHeadCell forIndexPath:indexPath];
        tHeadView.model = _model.mineData[indexPath.section];
        headView = tHeadView;
    }
    return headView;
}
#pragma mark - 懒加载

- (UICollectionView *)mineCollectionView
{
    if (!_mineCollectionView) {
        //布局
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 1;
        layout.minimumInteritemSpacing = 1;
        _mineCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT/2.8, SCREEN_WIDTH, SCREEN_HEIGHT-49-SCREEN_HEIGHT/2.8) collectionViewLayout:layout];
        _mineCollectionView.dataSource = self;
        _mineCollectionView.delegate = self;
        _mineCollectionView.backgroundColor = kBackgroundGray;
        _mineCollectionView.showsVerticalScrollIndicator = NO;

        //注册cell
        [_mineCollectionView registerNib:[UINib nibWithNibName:@"MineCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:MineItemCell];
        
        
        //注册head
        [_mineCollectionView registerNib:[UINib nibWithNibName:@"MineHeadCollectionView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:MineHeadCell];
    }
    return _mineCollectionView;
}

#pragma mark - private

- (void)setupNavigationBar
{
    //设置navigation透明
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    //设置tint颜色
    [self.navigationController.navigationBar setTintColor:kWhite];
    
    //设置按钮
    UIBarButtonItem *setBtn = [[UIBarButtonItem alloc] initWithImage:KImageNamed(@"m_sezhigray") style:UIBarButtonItemStyleDone target:self action:@selector(setClick)];
    
    //消息按钮
    UIBarButtonItem *messageBtn = [[UIBarButtonItem alloc] initWithImage:KImageNamed(@"m_xiaoxi") style:UIBarButtonItemStyleDone target:self action:@selector(messageClick)];
    
    self.navigationItem.rightBarButtonItems = @[messageBtn, setBtn];

}

- (void)headClick
{
    TT_Log(@"头像点击");
}

- (void)setClick
{
    TT_Log(@"setClick");
}

- (void)messageClick
{
    TT_Log(@"messageClick");
}

- (void)descriptionClick
{
    TT_Log(@"descriptionClick");
}
@end
